package com.yikes.srb.controller.web;

import com.alibaba.fastjson2.JSONObject;
import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.result.Result;
import com.yikes.srb.constant.hfb.RequestHelper;
import com.yikes.srb.leadingModle.form.lendReturn.LendReturnAddForm;
import com.yikes.srb.leadingModle.form.lendReturn.LendReturnEditForm;
import com.yikes.srb.leadingModle.form.lendReturn.LendReturnQuery;
import com.yikes.srb.leadingModle.vo.LendReturnVO;
import com.yikes.srb.service.LendReturnService;
import com.yikes.srb.web.utils.jwt.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 还款记录表 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@Slf4j
@RequiredArgsConstructor
@Tag(name = "LendReturnController", description = "还款记录表")
@RestController
@RequestMapping("/admin/lend_return")
public class LendReturnController {

    private final LendReturnService service;


    @Operation(description = "分页查询")
    @GetMapping("/list_page")
    public Result<PageResult<LendReturnVO>> listPage(LendReturnQuery query) {

        return Result.ok(service.listPage(query));
    }

    @Operation(description = "根据id查询单条信息")
    @GetMapping("/info/{id}")
    public Result<LendReturnVO> infoId(@PathVariable("id") Long id) {

        return Result.ok(service.infoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/create")
    public Result create(@Valid @RequestBody LendReturnAddForm form) {

        service.create(form);
        return Result.ok();
    }

    @Operation(description = "编辑")
    @PutMapping("/edit")
    public Result edit(@Valid @RequestBody LendReturnEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "获取列表")
    @GetMapping("/list/{lendId}")
    public Result list(
            @Parameter(name = "标的id", required = true)
            @PathVariable Long lendId) {

        List<LendReturnVO> list = service.selectByLendId(lendId);
        return Result.ok(list);
    }

    @Operation(description = "用户还款")
    @PostMapping("/auth/commitReturn/{lendReturnId}")
    public Result commitReturn(
            @Parameter(name = "还款计划id", required = true)
            @PathVariable Long lendReturnId, HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        String formStr = service.commitReturn(lendReturnId, userId);
        return Result.ok(formStr);
    }

    @Operation(description = "还款异步回调")
    @PostMapping("/notifyUrl")
    public String notifyUrl(HttpServletRequest request) {

        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        log.info("还款异步回调：" + JSONObject.toJSONString(paramMap));

        //校验签名
        if(RequestHelper.isSignEquals(paramMap)) {
            if("0001".equals(paramMap.get("resultCode"))) {

                service.notify(paramMap);
            } else {

                log.info("还款异步回调失败：" + JSONObject.toJSONString(paramMap));
                return "fail";
            }
        } else {

            log.info("还款异步回调签名错误：" + JSONObject.toJSONString(paramMap));
            return "fail";
        }
        return "success";
    }

}
